import React, { useState, useEffect } from 'react'
import TitleBack from "../../components/TitleBack/TitleBack"
import { reqgetcountdown, reqgetgroupon, reqgetindexgoods, reqgetnew, reqgetseckill } from '../../http/api'
import Goods from './components/Goods/Goods'
import Groupen from './components/Groupen/Groupen'
import New from './components/New/New'
import Seckill from './components/Seckill/Seckill'

export default function Home() {
    // 新人专享
    const [newList, setnewList] = useState([]);
    // 万人团
    const [grouponLIst, setgrouponLIst] = useState([]); 
    // 秒杀
    const [seckill, setseckill] = useState([]);
    // 倒计时
    const [countdown, setcountdown] = useState([]);
    // 首页商品信息
    const [indexgoods, setindexgoods] = useState([]);
    // 选项卡的n
    const [n, setN] = useState(0)


    useEffect(() => {
        // 新人专享
        reqgetnew().then(res => {
            if (res.data.code === 200) {
                setnewList(res.data.list)
            };
        });

        //  万人团
        reqgetgroupon().then(res => {
            if (res.data.code === 200) {
                setgrouponLIst(res.data.list)
            };
        });

        // 秒杀
        reqgetseckill().then(res => {
            if (res.data.code === 200) {
                setseckill(res.data.list)
            };
        });

        // 倒计时
        reqgetcountdown().then(res => {
            if (res.data.code === 200) {
                setcountdown(res.data.list)
            };
        })

        // 首页商品信息
        reqgetindexgoods().then(res => {
            if (res.data.code === 200) {
                setindexgoods(res.data.list)
            }
        })
    }, []);
    
    return (
        <div>
            <TitleBack title={"首页"}></TitleBack>
            {/* 新人专享 */}
            {newList.length > 0 ? <New newList={newList}></New> : null}
            {/* 万人团 */}
            {grouponLIst.length > 0 ? <Groupen grouponLIst={grouponLIst}></Groupen> : null}
            {/* 秒杀 */}
            {seckill.length > 0 ? <Seckill seckill={seckill}></Seckill> : null}
            {/* 首页商品信息 */}
            {indexgoods.length>0?<Goods showgoods={indexgoods[n].content} n={n} setN={(a)=>setN(a)}></Goods>:[]}
        </div>
    )
}
